<?php
namespace Orm\User;

use Base\Exception\RmException;
class UserClass extends \Base\BaseOrmObject{
    protected   $id,
				$nickname = '', // 昵称
				$openid = '', // 普通用户的标识
				$province = '', // 省
				$city = '', // 市
				$country = '', // 区
				$headimgurl = '', // 头像
				$sex = 0, // 1，男性，2，女性
				$is_del = 0,
				$insert_time = '',
				$update_time = '';

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\User\UserModel();
        $data = $model->loadById($id);
        self::__init($data);
    }
    public function loadByOpenId($openid = '')
    {
        $model = new \Orm\User\UserModel();
        $data = $model->loadByOpenId($openid);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
			'nickname' => $this->nickname,
			'openid' => $this->openid,
			'province' => $this->province,
			'city' => $this->city,
			'country' => $this->country,
			'headimgurl' => $this->headimgurl,
			'sex' => $this->sex,
        );

        $model = new \Orm\User\UserModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
			'nickname' => $this->nickname,
			'openid' => $this->openid,
			'province' => $this->province,
			'city' => $this->city,
			'country' => $this->country,
            'headimgurl' => $this->headimgurl,
			'sex' => $this->sex,
        );

        $model = new \Orm\User\UserModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\User\UserModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}